//
//  WCNCommentVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/2.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNCommentVC.h"
#import "WCDrawBorderView.h"
//
#import "WCNCommentItemTCell.h"
//
#import "UILabel+Utilities.h"
//
#import "Macros.h"

static NSString *cellId = @"WCNCommentItemTCell";

@interface WCNCommentVC ()

@property (strong, nonatomic) UIView *commentFooterView;
@property (nonatomic) float footerHeight;

@end

@implementation WCNCommentVC

- (instancetype)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        self.title = @"评价详情";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = kAppBg;
    
    self.tableView.separatorColor = kBorderColor;
    [self.tableView registerNib:[UINib nibWithNibName:cellId bundle:nil] forCellReuseIdentifier:cellId];
    self.tableView.rowHeight = 51;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNCommentItemTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    switch (indexPath.row) {
        case 0:{
            cell.name.text = @"总体满意度";
            [cell setScoreValue:[[self.comment valueForKey:@"ratingGeneral"] floatValue]];
            break;
        }
        case 1:{
            cell.name.text = @"服务态度";
            [cell setScoreValue:[[self.comment valueForKey:@"ratingAttitude"] floatValue]];
            break;
        }
        case 2:{
            cell.name.text = @"响应速度";
            [cell setScoreValue:[[self.comment valueForKey:@"ratingPunctuality"] floatValue]];
            break;
        }
        case 3:{
            cell.name.text = @"专业程度";
            [cell setScoreValue:[[self.comment valueForKey:@"ratingExpertise"] floatValue]];
            break;
        }
        default:
            break;
    }
    
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.commentFooterView) {
        return self.footerHeight;
    }
    
    WCDrawBorderView *footerView = [[WCDrawBorderView alloc] init];
    footerView.backgroundColor = [UIColor whiteColor];
    footerView.leftBorder = NO;
    footerView.rightBorder = NO;
    footerView.topBorder = NO;
    footerView.bottomBorder = YES;
    footerView.borderColor = kBorderColor;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    titleLabel.text = @"评价";
    titleLabel.font = [UIFont systemFontOfSize:15.0];
    titleLabel.textColor = kTxtColor;
    [titleLabel addConstraint:[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeHeight
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:nil attribute:NSLayoutAttributeNotAnAttribute
                                                          multiplier:1 constant:30]];
    
    [footerView addSubview:titleLabel];
    [footerView addConstraint:[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeLeading
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:footerView attribute:NSLayoutAttributeLeading
                                                          multiplier:1 constant:15]];
    [footerView addConstraint:[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeTop
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:footerView attribute:NSLayoutAttributeTop
                                                          multiplier:1 constant:5]];
    
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.translatesAutoresizingMaskIntoConstraints = NO;
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = [UIColor blackColor];
    contentLabel.font = [UIFont systemFontOfSize:16.0];
    
    [footerView addSubview:contentLabel];
    [footerView addConstraint:[NSLayoutConstraint constraintWithItem:footerView attribute:NSLayoutAttributeTrailing
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:contentLabel attribute:NSLayoutAttributeTrailing
                                                          multiplier:1 constant:15]];
    [footerView addConstraint:[NSLayoutConstraint constraintWithItem:footerView attribute:NSLayoutAttributeBottom
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:contentLabel attribute:NSLayoutAttributeBottom
                                                          multiplier:1 constant:10]];
    
    [footerView addConstraint:[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeLeading
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:contentLabel attribute:NSLayoutAttributeLeading
                                                          multiplier:1 constant:0]];
    [footerView addConstraint:[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeBottom
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:contentLabel attribute:NSLayoutAttributeTop
                                                          multiplier:1 constant:-5]];
    
    contentLabel.text = [self.comment valueForKey:@"comment"];
    [contentLabel showAttributedText:0 lineSpace:6];
    
    NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:footerView attribute:NSLayoutAttributeWidth
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:nil attribute:NSLayoutAttributeNotAnAttribute
                                                                      multiplier:1 constant:kScreenWidth];
    [footerView addConstraint:widthConstraint];
    
    CGSize size = [footerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    self.footerHeight = size.height + 15;
    self.commentFooterView = footerView;
    
    [footerView removeConstraint:widthConstraint];
    
    return self.footerHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return self.commentFooterView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

@end
